﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BudgetSV.Common;
using BudgetData;

namespace BudgetSV.admin.manager
{
    public partial class depart_edit : ManagePage
    {
        private string action = DTEnums.ActionEnum.Add.ToString(); //操作类型
        private long id = 0;

        protected void Page_Load(object sender, EventArgs e)
        {
            string _action = DTRequest.GetQueryString("action");
            this.id = DTRequest.GetQueryInt("id");

            if (!string.IsNullOrEmpty(_action) && _action == DTEnums.ActionEnum.Edit.ToString())
            {
                this.action = DTEnums.ActionEnum.Edit.ToString();//修改类型
                if (this.id == 0)
                {
                    JscriptMsg("传输参数不正确！", "back", "Error");
                    return;
                }
                
            }
            if (!Page.IsPostBack)
            {
                ChkAdminLevel("depart_list", DTEnums.ActionEnum.View.ToString()); //检查权限
                DepartTreeBind(); //绑定部门列表
                if (action == DTEnums.ActionEnum.Edit.ToString()) //修改
                {
                    ShowInfo(this.id);
                }
                else
                {
                    if (this.id > 0)
                    {
                        this.ddlParentId.SelectedValue = this.id.ToString();
                    }
                    txtTitle.Attributes.Add("ajaxurl", "../../tools/admin_ajax.ashx?action=depart_validate");
                }
            }
        }

        #region 绑定部门列表=============================

        private void DepartTreeBind()
        {
            var dpmd = GetAllList(GetAdminInfo().org_id);
            foreach (var d in dpmd)
            {

                string Id = d.id.ToString();
                int ClassLayer = int.Parse(d.class_layer.ToString());
                string Title = d.name.ToString().Trim();

                if (ClassLayer == 1)
                {
                    this.ddlParentId.Items.Add(new ListItem(d.name, d.id.ToString()));
                }
                else
                {
                    Title = "├ " + Title;
                    Title = Utils.StringOfChar(ClassLayer - 1, "　") + Title;
                    this.ddlParentId.Items.Add(new ListItem(Title, Id));
                }
            }
        }
        #endregion

        #region 赋值操作=================================

        private void ShowInfo(long _id)
        {
            Organization model = Mod.GetModel<Organization>(c=>c.id==_id);

            ddlParentId.SelectedValue = model.parent_id.ToString();
            txtSortId.Text = model.sort_id.ToString();
            if (model.is_lock == 1)
            {
                cbIsLock.Checked = true;
            }
            txtTitle.Text = model.name;
            txtTitle.Attributes.Add("ajaxurl", "../../tools/admin_ajax.ashx?action=depart_validate&old_name=" + Utils.UrlEncode(model.name));
            txtTitle.Focus(); //设置焦点，防止JS无法提交
            if (model.is_sys == 1)
            {
                ddlParentId.Enabled = false;
                txtTitle.ReadOnly = true;
            }
            txtTitle.Text = model.name;
            txtRemark.Text = model.remark;
        }
        #endregion

        #region 增加操作=================================

        private bool DoAdd()
        {
            //try
            //{
                Organization model = new Organization();
                model.name = txtTitle.Text.Trim();
                model.sort_id = int.Parse(txtSortId.Text.Trim());
                model.is_lock = 0;
                if (cbIsLock.Checked == true)
                {
                    model.is_lock = 1;
                }
                model.remark = txtRemark.Text.Trim();
                model.parent_id = int.Parse(ddlParentId.SelectedValue);
            model = Mod.Add(model);
                if (model.id > 0)
                {
                    this.id = model.id;
                    AddAdminLog(DTEnums.ActionEnum.Add.ToString(), "添加部门信息:" + model.name); //记录日志
                    return true;
                }
            //}
            //catch
            //{
            //    return false;
            //}
            return false;
        }
        #endregion

        #region 修改操作=================================

        private bool DoEdit(long _id)
        {
            //try
            //{
            Organization model = Mod.GetModel<Organization>(c => c.id == _id);

            model.name = txtTitle.Text.Trim();
                model.sort_id = int.Parse(txtSortId.Text.Trim());
                model.is_lock = 0;
                if (cbIsLock.Checked == true)
                {
                    model.is_lock = 1;
                }
                model.remark = txtRemark.Text.Trim();
                if (model.is_sys == 0)
                {
                    int parentId = int.Parse(ddlParentId.SelectedValue);
                    //如果选择的父ID不是自己,则更改
                    if (parentId != model.id)
                    {
                        model.parent_id = parentId;
                    }
                }

                if (Mod.Update(model)>0)
                {
                    AddAdminLog(DTEnums.ActionEnum.Add.ToString(), "修改部门信息:" + model.name); //记录日志
                    return true;
                }
            //}
            //catch
            //{
            //    return false;
            //}
            return false;
        }
        #endregion

        //保存
        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            if (action == DTEnums.ActionEnum.Edit.ToString()) //修改
            {
                ChkAdminLevel("depart_list", DTEnums.ActionEnum.Edit.ToString()); //检查权限
                if (!DoEdit(this.id))
                {
                    JscriptMsg("保存过程中发生错误！", "", "Error");
                    return;
                }
                JscriptMsg("修改部门成功！", "depart_list.aspx", "Success", "parent.loadMenuTree(true)");
            }
            else //添加
            {
                //ChkAdminLevel("app_department_list", DTEnums.ActionEnum.Add.ToString()); //检查权限
                ChkAdminLevel("depart_list", DTEnums.ActionEnum.Add.ToString()); //检查权限
                if (!DoAdd())
                {
                    JscriptMsg("保存过程中发生错误！", "", "Error");
                    return;
                }
                JscriptMsg("添加部门成功！", "depart_list.aspx", "Success", "parent.loadMenuTree(true)");
            }
        }

    }
}